<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\Request;
use think\Validate;
use think\Db;
use think\Session;
use app\admin\model\Smspro as Smspros;
use app\admin\model\Smstasklog as Smstasklogs;
use app\admin\model\Smssendlog as Smssendlogs;
use app\admin\model\Smsresultlog as Smsresultlogs;
use app\admin\model\Smscfg as Smscfgs;

function login($code,$config_session)
{
    //获取appid和appsecret
    $wxconfig = Db::name('wxconfig')->find();
    $appId = $wxconfig['appid'];
    $AppSecret = $wxconfig['appsecret'];
    $authcode2Sessionhost = $wxconfig['authcode2Sessionhost'];
    $js_code = $code;
    $url = $authcode2Sessionhost.'?appid='.$appId.'&secret='.$AppSecret.'&js_code='.$js_code.'&grant_type=authorization_code';
    $res = file_get_contents($url);
    $res = json_decode($res,true);

    //将openid和session_key存到session里
    $session = new Session($config_session);
    $session->prefix();
    $session->set('openid',$res['openid'],$config_session['prefix']);
    $session->set('session_key',$res['session_key'],$config_session['prefix']);

    //查询库里是否已经存在
    $user = Db::name('user')->where(array('openid'=>$res['openid']))->find();
    if(empty($user)){
        //信息入库
        $insertData = array(
            'openid' => $res['openid'],
            'session_key' => $res['session_key'],
            'first_visit_time' => time(),
            'last_visit_time' => time(),
            'visit_count' => 1,
        );
        Db::name('user')->insert($insertData);
    }else{
        $count = $user['visit_count'] + 1;
        $updateData = array(
            'session_key' => $res['session_key'],
            'last_visit_time' => time(),
            'visit_count' => $count,
        );
        Db::name('user')->where('id',$user['id'])->update($updateData);
    }
    if($res != false){
        return ['code'=>0,'msg'=>'成功'];
    }else{
        return ['code'=>1,'msg'=>'失败'];
    }
}
/**
 * 获取流水号
 */
function get_serial_number($try_number = 1){
    return date('YmdHis', time()) . mt_rand(1000,9999) . sprintf("%01d", $try_number);
}
/**
 * 获取客户端IP
 */
function get_client_ip() {
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    }
    elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');

    }
    elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    }
    elseif (getenv("REMOTE_ADDR")){
        $ip = getenv("REMOTE_ADDR");
    }else{
        $ip = "Unknow";
    }
    return $ip;
}
/**
 * 检测号码
 */
function checkMobile($mobile){
    $regex = "/13[0-9]\d{8}|14[0-9]\d{8}|15[0-9]\d{8}|16[0-9]\d{8}|17[0-9]\d{8}|18[0-9]\d{8}|19[0-9]\d{8}/";
    return preg_match($regex,$mobile);
}

/**
 * 短信发送
 */
function sendSms($data){
    try {
        if(!$data){
            return abort(409,'失败');
        }elseif(!($api = Smspros::get(['provider_id'=>$data['provider_id'],'is_valid'=>1]))){
            $send_ret = array(
                'provider_id'           => $data['provider_id'],     //在md_provider 表中
                'task_id'               => $data['task_id'],
                'provider_request_id'   => '',                      //是供应商的订单编号
                'provider_msg_id'       => 'yh'. $data['task_id'],   //是供应商返回的回执id，可据此查询状态
                'status'                => 996,                     //我方定义的状态值
                'message'               => '没有该供应商的账号信息',//我方定义的状态信息
                'provider_code'         => '',                      //供应商定义的结果值
                'provider_message'      => '' ,                     //供应商的结果值+信息
                'return_time'           => time(),                	//提交后返回时间
            );
        }else{
            //修改任务记录状态
            if(Smstasklogs::update(['status'=>0], ['client_id'=>$data['client_id'],'task_id'=>$data['task_id'],'order_id'=>$data['order_id']])){
                $req =array(
                    'task_id'       => $data['task_id'],
                    'mobile'        => $data['mobile'],
                    'provider_id'   => $data['provider_id'],
                    'client_id'     => $data['client_id'],
                    'content'       => $data['content'],
                    'signature'     => $data['signature'],
                    'at_time'       => $data['at_time'],
                    'ext_code'      => $data['ext_code'],
                    'template_code' => $data['template_code'],
                    'template_param'=> $data['template_param'],
                );
                $send_time = time();
                switch ($data['provider_id']){
                    case 12:
                        $send_ret = \app\api\controller\Apialysms::send($req,$api);
                        break;
                    case 39:
                        $send_ret = \app\api\controller\Apichuanglan::send($req,$api);
                        break;
                    case 40:
                    case 41:
                    case 42:
                    case 43:
                        $send_ret = \app\api\controller\Apidahansms::send($req,$api);
                        break;
                    case 45:
                        $send_ret = \app\api\controller\Apijxjm::send($req,$api);
                        break;
                    default:
                        //如果没有找到通道，返回996
                        $send_ret = array(
                            'provider_id'           => $data['provider_id'],     //在md_provider 表中
                            'task_id'               => $data['task_id'],
                            'provider_request_id'   => '',                      //是供应商的订单编号
                            'provider_msg_id'       => 'yh'. $data['task_id'],   //是供应商返回的回执id，可据此查询状态
                            'status'                => 996,                     //我方定义的状态值
                            'message'               => '没有匹配的供应商通道',  //我方定义的状态信息
                            'provider_code'         => '',                      //供应商定义的结果值
                            'provider_message'      => '' ,                     //供应商的结果值+信息
                            'return_time'           => time(),                	//提交后返回时间
                        );
                }
            }
        }
        //保存响应结果
        $validMobiles = explode(',',$data['mobile']);
        foreach($validMobiles as $v){
            if($data['sms_size'] == 1){
                $insertData = array(
                    'provider_id'           => $send_ret['provider_id'],
                    'request_id'            => $send_ret['task_id'].$v.$data['sms_size'],
                    'provider_request_id'   => $send_ret['provider_request_id'],
                    'provider_msg_id'       => $send_ret['provider_msg_id'],
                    'provider_msg_id_mobile'=> $send_ret['provider_msg_id'].$v.$data['sms_size'],
                    'code'                  => $send_ret['status'],
                    'message'               => $send_ret['message'],
                    'provider_code'         => $send_ret['provider_code'],
                    'provider_message'      => $send_ret['provider_message'],
                    'send_time'             => $send_time ,
                    'return_time'           => $send_ret['return_time'],
                    'provider_template_code'=> $data['template_code'],
                    'provider_template_param'=> $data['template_param'],
                    'remark'                => 2
                );
                Smssendlogs::create($insertData);
            }elseif ($data['sms_size'] > 1){
                for($j = 1;$j<=$data['sms_size'];$j++){
                    $insertData = array(
                        'provider_id'           => $send_ret['provider_id'],
                        'request_id'            => $send_ret['task_id'].$v.$j,
                        'provider_request_id'   => $send_ret['provider_request_id'],
                        'provider_msg_id'       => $send_ret['provider_msg_id'],
                        'provider_msg_id_mobile'=> $send_ret['provider_msg_id'].$v.$j,
                        'code'                  => $send_ret['status'],
                        'message'               => $send_ret['message'],
                        'provider_code'         => $send_ret['provider_code'],
                        'provider_message'      => $send_ret['provider_message'],
                        'send_time'             => $send_time ,
                        'return_time'           => $send_ret['return_time'],
                        'provider_template_code'=> $data['template_code'],
                        'provider_template_param'=> $data['template_param'],
                        'remark'                => 2
                    );
                    Smssendlogs::create($insertData);
                }
            }
        }
        if(Smstasklogs::update(['status'=>1], ['client_id'=>$data['client_id'],'task_id'=>$data['task_id'],'order_id'=>$data['order_id']])){
            //提交失败，直接记录到result_log中
            if($send_ret['status'] == 996){
                foreach ($validMobiles as $w){
                    if($data['sms_size'] == 1){
                        $send_fas = array(
                            'provider_id'               => $send_ret['provider_id'],
                            'mobile'                    => $w,
                            'success'                   => 0,
                            'task_id'                   => $send_ret['task_id'],
                            'provider_msg_id'           => $send_ret['provider_msg_id'],
                            'provider_msg_id_mobile'    => $send_ret['provider_msg_id'].$w.$data['sms_size'],
                            'provider_notify_time'      => time(),
                            'provider_report_time'      => time(),
                            'status_code'               => $send_ret['status'],
                            'status_message'            => $send_ret['message'],
                            'provider_status_code'      => $send_ret['provider_code'],
                            'provider_status_message'   => $send_ret['provider_message'],
                            'sms_size'                  => $data['sms_size'],
                            'notify_time'               => time()
                        );
                        Smsresultlogs::create($send_fas);
                    }elseif ($data['sms_size'] > 1){
                        for($n = 1;$n<=$data['sms_size'];$n++){
                            $send_fas = array(
                                'provider_id'               => $send_ret['provider_id'],
                                'mobile'                    => $w,
                                'success'                   => 0,
                                'task_id'                   => $send_ret['task_id'],
                                'provider_msg_id'           => $send_ret['provider_msg_id'],
                                'provider_msg_id_mobile'    => $send_ret['provider_msg_id'].$w.$n,
                                'provider_notify_time'      => time(),
                                'provider_report_time'      => time(),
                                'status_code'               => $send_ret['status'],
                                'status_message'            => $send_ret['message'],
                                'provider_status_code'      => $send_ret['provider_code'],
                                'provider_status_message'   => $send_ret['provider_message'],
                                'sms_size'                  => $data['sms_size'],
                                'notify_time'               => time()
                            );
                            Smsresultlogs::create($send_fas);
                        }
                    }
                }
                //退款
                Smscfgs::where('id='.$data['cfg_id'])->setInc('balance', abs($data['num']));
                //提交失败，直接返回
                return $send_ret;
            }
        }
        return $send_ret;
    } catch (\Exception $e) {
        // 捕获异常并转发为HTTP异常
        return abort(404, $e->getMessage());
    }
}
function sms_handle_callback($cb){
    //获取该记录
    if(!empty($cb['mobile'])&&!empty($cb['task_id'])){
        $rsrl = \think\Db::name('view_sms_req_send_res_log')->where(['mobile'=>$cb['mobile'],'task_id'=>$cb['task_id']])->find();
    }elseif (!empty($cb['mobile'])&&!empty($cb['provider_msg_id'])){
        $rsrl = \think\Db::name('view_sms_req_send_res_log')->where(['mobile'=>$cb['mobile'],'sen_provider_msg_id'=>$cb['provider_msg_id']])->find();
    }
    $cfg = Smscfgs::get(['client_id' => $rsrl['client_id'],'sms_type_id'=>$rsrl['sms_type_id'],'status'=>1]);

    //将结果存入sms_result_log
    $ret = array (
        'provider_id'       =>$cb['provider_id'],
        'mobile'            =>$cb['mobile'],
        'success'           => $cb['status_code'] == 0 ? 1 : 0,
        'task_id'           => $cb['task_id'],
        'provider_msg_id'   => $cb['provider_msg_id'],
        'provider_msg_id_mobile' => $cb['provider_msg_id'].$cb['mobile'].$cb['sms_index'],
        'provider_notify_time'  => $cb['provider_notify_time'],
        'provider_report_time'  => $cb['provider_report_time'],
        'status_code'           => $cb['status_code'],
        'status_message'        => $cb['status_message'],
        'provider_status_code'  => $cb['provider_status_code'],
        'provider_status_message' => $cb['provider_status_message'],
        'sms_size'              => $cb['sms_size'],
        'sms_index'             => $cb['sms_index'],
        'notify_time'           => $cb['notify_time'],
    );
    Smsresultlogs::create($ret);
    //如果失败，给客户退款，因为长短信都拆分为多条，一条一条地入库，所以每次退款，就只退款1条
    if($cb['status_code'] == 1 || $cb['status_code'] == 2){
        Smscfgs::where('id='.$cfg['id'])->setInc('balance', 1);
    }
    if($cfg['need_callback'] == 1){
        //异步回调
        $client_cb = array(
            'orderId'   => $rsrl['order_id'],
            'msgid'     => $cb['provider_msg_id'],
            'reportTime' => $cb['provider_report_time'],
            'notifyTime' => $cb['notify_time'],
            'mobile'    => $cb['mobile'],
            'smsSize'   => $cb['sms_size'],
            'smsIndex'  => $cb['sms_index'],
            'code'      => $cb['status_code'],
            'smgCode'   => $cb['provider_status_code'],
            'message'   => $cb['status_message'],
            'report_callback_url' => $cfg['report_callback_url'],
            'mo_callback_url' => $cfg['mo_callback_url'],
        );
        sms_callback_client($client_cb);
    }
}
function sms_callback_client($cb){
    $data = array(
        'orderId'    => $cb['orderId'],
        'msgid'      => $cb['msgid'],
        'reportTime' => $cb['reportTime'],
        'notifyTime' => $cb['notifyTime'],
        'mobile'     => $cb['mobile'],
        'smsSize'    => $cb['smsSize'],
        'smsIndex'   => $cb['smsIndex'],
        'code'       => $cb['code'],
        'smgCode'    => $cb['smgCode'],
        'message'    => $cb['message'],
    );
    $url = $cb['report_callback_url'];
    if(!empty($url)&&!empty($data)){
        //get请求,默认的
        $ch = curl_init(); //创建curl资源

        curl_setopt($ch,CURLOPT_URL,$url);

        curl_setopt($ch,CURLOPT_HTTPHEADER,array(
            'Cont_Type : application/x-www-form-urlencoded;charset=utf-8',//默认，key=value$key=value
        ));//一个用来设置HTTP头字段的数组。使用如下的形式的数组进行设置： array('Content-type: text/plain', 'Content-length: 100')

        curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));

        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);

        curl_setopt($ch,CURLOPT_POST,1);//启用时会发送一个常规的POST请求，类型为：application/x-www-form-urlencoded，就像表单提交的一样。

        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//禁用后cURL将终止从服务端进行证书验证
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//禁用后cURL将终止从服务端进行证书验证

        $output = curl_exec($ch);

        if($output){
            echo  curl_error($ch);
        }
        $info = curl_getinfo($ch);

        curl_close($ch);
        if($info['http_code'] == 200){
            //响应成功
        }
    }
}
function sms_mo_handle_callback($cb){
    //匹配客户，首先根据优辉云平台给客户配置的子码匹配客户，若没有子码，再根据签名匹配客户
    if(!empty($cb['sp_code'])){
        $re = \think\Db::name('sms_client_cfg')->where(['ext_code'=>$cb['sp_code']])->field('client_id')->find();
    }else{
        $re = \think\Db::name('sms_signature')->where(['signature'=>$cb['signature']])->field('client_id')->find();
    }
    //存入上行表
    $mo = array(
        'client_id'     => $re['client_id'],
        'provider_id'   => $cb['provider_id'],
        'receiver'      => $cb['receiver'],
        'pswd'          => $cb['pswd'],
        'mo_time'       => $cb['mo_time'],
        'mobile'        => $cb['mobile'],
        'content'       => $cb['content'],
        'signature'     => $cb['signature'],
        'send_time'     => $cb['send_time'],
        'dest_code'     => $cb['dest_code'],
        'sp_code'       => $cb['sp_code'],
        'sequence_id'   => $cb['sequence_id'],
        'is_ems'        => $cb['is_ems'],
        'ems_head'      => $cb['ems_head'],
        'create_time'   => time(),
    );
    \think\Db::name('sms_inbox')->insert($mo);

}
function handle_callback($cb){
    $table = 'view_putting_result_callback';
    if(!empty($cb['serial_number']))
        $db_cb = \think\Db::name($table)->where('serial_number',$cb['serial_number'])->where('provider_id',$cb['provider_id'])->find();
    else{
        if(!empty($cb['provider_trans_id']))
            $db_cb = \think\Db::name($table)->where('provider_trans_id',$cb['provider_trans_id'])->where('provider_id',$cb['provider_id'])->find();
    }
    /*
                $db_cb = \think\Db::name($table)
             ->where('serial_number = :sn OR provider_trans_id = :trand_id ', ['sn' => $cb['serial_number'], 'trand_id' => $cb['provider_trans_id']])
             ->where('provider_id',$cb['provider_id'])
             ->find();
    */
//		echo \think\Db::name($table)->getLastSql();
    if(empty($db_cb))
        return;
    $auto_rebuy = $db_cb['auto_rebuy'];
    $try_number = $db_cb['try_number'];
    $org_serial_number = $db_cb['serial_number'];

    //如果try_number>1，则原始order_sn是去掉'-'和数字
    if( $try_number <=1)
        $org_order_sn = $db_cb['order_sn'];
    else
        $org_order_sn= substr($db_cb['order_sn'],0, strlen($db_cb['order_sn']) - strlen($try_number) -1);
    //检查有无反馈“维护”
    if (strpos($cb['result_message'],'维护') >0)
        \think\Db::name('provider_package')->where('id',$db_cb['provider_package_id'])->update(['is_valid' => 0]);
    //检查供应商返回有无价格
    $cost_price = $cb['cost_price'];
    //cost_price不为零且充值成功的才检查更新
    if((!empty($cost_price)) && $cb['status'] == 1 ) {
        $cost_price = (double)$cost_price;
        $pvd_pkg = \think\Db::name('provider_package')->where('id',$db_cb['provider_package_id'])->find();
        //如果供应商返回的价格大于数据库中设置的价格，则更新数据库中的套餐价格，避免以后造成损失
        if($cost_price != $pvd_pkg['cost_price'] && $cost_price >0){
            $pvd_pkg = \think\Db::name('provider_package')->where('id',$db_cb['provider_package_id'])->exp('discount',$cost_price . '/par_value')->update(['cost_price' => $cost_price]);
        }
    }
    else //若供应商没有返回价格，则取数据库中的价格
        $cost_price = (double)($db_cb['cost_price']);
    //如果成功消耗cost_price，否则消耗0
    if( $cb['status'] == 1){
        $price  = $cost_price;
        $act_client_price = $db_cb['client_price']; //实际扣除企业客户的金额
    }
    else{
        $price  = 0;
        $act_client_price = 0; ////实际扣除企业客户的金额
    }
    $ret = array (
        'status' =>$cb['status'],
        'status_code' =>$cb['status_code'],
        'provider_id' => $cb['provider_id'],
        'serial_number' => $cb['serial_number'],
        'provider_trans_id' => $cb['provider_trans_id'],
        'result' => $cb['result'],
        'result_code' => $cb['result_code'],
        'result_message' => $cb['result_message'],
        'cost_price' => $price,
        'act_client_price' => $act_client_price,
        'cb_mobile' => $db_cb['mobile'],
        'ret_str' => $cb['ret_str'],
        'return_time' => $cb['return_time'],
    );
//		dump($ret);
    //记入结果日志表
    \think\Db::name('putting_result_log')->insert($ret);
    //////////////////////////////////////////////////////////////////////////////////////
    //处理重充值_begin
    //如果充值失败且需要失败重充值，则尝试重充值
    if( ($cb['status'] != 1) && $auto_rebuy && (!strpos($cb['result_message'],'停机'))){
        /////////////////////////////////////////////////////////////////////////////////////
        //找到当前serial_number已发送的provider_id，用以剔除再提交
        $sent = \think\Db::name('putting_send_times_tmp')
            ->where("INSTR(serial_number,'". substr( $org_serial_number ,0,  strlen( $org_serial_number) -2 ) ."')>0")
            ->order("try_number DESC")
            ->select();
//			echo \think\Db::name('putting_send_times_tmp')->getLastSql();
        $try_number = $sent[0]['try_number'] ; //DESC，最大的一个try_number
        //最多只尝试充值9次
        $max_try_number = \think\Config::get('max_try_number') <=9 ? \think\Config::get('max_try_number') :9;
        if($try_number <  $max_try_number){
            $pvd_pkg_ids='';
            //组装待剔除的provider_package_id，英文逗号分隔
            foreach ($sent as $s){
                if(!$pvd_pkg_ids)
                    $pvd_pkg_ids .= $s['provider_package_id'];
                else
                    $pvd_pkg_ids .= ',' . $s['provider_package_id'];
            }
            //获取可用的供应商套餐
            $available_pkg = find_flow_package($db_cb['package_type'],$db_cb['package'],$db_cb['biz_id'],$db_cb['mobile'],$db_cb['client_price'],'',0,$pvd_pkg_ids);
            //如果有可用供应商套餐，发起重新充值，剔除已发的供应商套餐
            if ($available_pkg){
                $origin_id = 7; //充值途径id，7 = 回执失败自动重充
                //先将原serial_number的rebought改为1已重充值
                $rebt = array('rebought'=>1);
                \think\Db::name('putting_send_log')->where("serial_number",$db_cb['serial_number'])->update($rebt);
                if(!$client)
                    $client = \think\Db::name('client')->where('id',$db_cb['client_id'])->find();

                $req = array(
                    'origin_id' => $origin_id,  //充值途径id，7 = 回执失败重充
                    'order_sn' => $org_order_sn. '-' . ($try_number+1),
                    'serial_number' => substr( $org_serial_number ,0,  strlen( $org_serial_number) -2 ) . sprintf("%02d", $try_number),
                    //					'order_sn' => $order_sn,
                    //					'serial_number' =>  $org_serial_number,
                    'biz_id' => $db_cb['biz_id'],
                    'card_id' => $db_cb['card_id'],  //?????
                    'product_code' => $db_cb['product_code'],
                    'mobile' => $db_cb['mobile'],
                    'callback_url' => $db_cb['callback_url'],
                    'package_type' => $db_cb['package_type'],
                    'try_number' => $try_number ,
                    'provider_package_id_exclude' =>  $pvd_pkg_ids,
                );
                \app\api\controller\Api::_inner_buy($req,$client);
                return ;  //返回，不回调，等下一次回调
            }
            else{ //失败且未找到可用供应商套餐，则对企业退款
                if(	$db_cb['payment_type_id'] == 1){
                    $refund = \think\Db::name('client_journal')->where('serial_number',$db_cb['serial_number'])->where('charge_type_id',1)->find();
                    //只对未退款的记录退款（不能重复退款）
                    if(!$refund){
                        //退款（即将企业余额增加）
                        \think\Db::name('client')->where('id',$db_cb['client_id'])->setInc('balance',$db_cb['client_price']);
                        //记录企业账户流水
                        $client = \think\Db::name('client')->field('balance')->where('id',$db_cb['client_id'])->find();
                        $journal = array(
                            'client_id' => $db_cb['client_id'],
                            'biz_id' => $db_cb['biz_id'],
                            'origin_id' => $db_cb['origin_id'],
                            'charge_type_id' => 1, //表示退款
                            'order_sn' => $db_cb['order_sn'],
                            'serial_number' =>  $db_cb['serial_number'],
                            'fee' => $db_cb['client_price'],
                            'balance' => $client['balance'],
                            'create_time' => time(),
                        );
                        \think\Db::name('client_journal')->insert($journal);
                    } //if(!$refund
                }//if(	$db_cb['payment_type_id'] == 1)
            }
        } //if($try_number < \think\Config::get('max_try_number') )
    }
    //处理重充值_end
    //////////////////////////////////////////////////////////////////////////////////////

    //异步触发，向企业客户回调
    $client_price = $db_cb['client_price'] ;
    if($cb['status'] !=1){
        $client_price = 0;
        if($cb['status'] == 5)
            $cb['status_code'] = 5;
    }

    $client_cb = array(
        'try_number' => $try_number,
        'callback_url' => $db_cb['callback_url'],
        'custom_callback' => $db_cb['custom_callback'],
        'order_sn' => $db_cb['order_sn'],
        'serial_number' => $db_cb['serial_number'],
        'product_code' => $db_cb['product_code'],
        'package_name' => $db_cb['package_name'],
        'provider_id' => $db_cb['provider_id'],
        'mobile' => $db_cb['mobile'],
        'fee' => $client_price,
        'result' => $cb['status'],
        'result_code' => $cb['status_code'] ,
        'result_msg' => result_msg($cb['status']),
    );
    callback_client($client_cb);

}
